package com.huimin.merchant.controller;

import com.huimin.merchant.common.util.SecurityUtil;
import com.huimin.merchant.dto.AppDTO;
import com.huimin.merchant.service.AppServiceApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author amx
 * @date 2020/12/3 18:26
 */
@RestController
@Api(tags = "商户应用接口")
public class AppController {

	@Reference
	private AppServiceApi appService;

	@ApiOperation("添加商户应用")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "商户应用的参数对象",
					name = "appDTO", required = true, dataType = "AppDTO",
					paramType = "body")
	})
	@PostMapping("/my/apps")
	public AppDTO createApp(@RequestBody AppDTO appDTO) {

		// 生成一个MerchantId,即当前token
		Long merchantId = SecurityUtil.getMerchantId();
		System.out.println(merchantId);
		// 将merchantId放入appDto
		appDTO.setMerchantId(merchantId);

		// 调用service，创建（并保存）app信息
		return appService.createApp(appDTO);
	}

	@ApiOperation("根据商户Id获取应用列表(获取商户应用信息)")
	@GetMapping(value = "/my/apps")
	public List<AppDTO> findAppsByMerchantId() {

		// 获取当前merchantId
		Long merchantId = SecurityUtil.getMerchantId();
		// 调用方法 得到应用列表
		return appService.findAppsByMerchantId(merchantId);
	}

	@ApiOperation("根据appid获取应用的详细信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "appId", value = "商户应用id", required = true, dataType = "String", paramType =
					"path")})
	@GetMapping(value = "/my/apps/{appId}")
	public AppDTO findAppById(@PathVariable("appId") Long appId) {
		return appService.findAppById(appId);
	}

}
